package com.weiyuwqj.api.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;

import javax.imageio.ImageIO;

/**
 * @author cx
 * @className:ImageUtil
 * @Description: 图片水印处理
 * @date:2017年3月27日上午10:39:50
 */
public class ImageUtil {

    /**
     * @param pressImg  水印文件
     * @param targetImg 目标文件
     * @param x
     * @param y
     * @param type      1 水印 2模糊
     */
    public static void pressImage(String pressImg, String targetImg,
                                  int x, int y, int screenshotX, int screenshotY, final int type) {
        try {
            File file = new File(targetImg);
            Image src = ImageIO.read(file);
            int width = src.getWidth(null);
            int height = src.getHeight(null);
            BufferedImage image = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = image.createGraphics();
            g.drawImage(src, 0, 0, width, height, null);
            URL url = new URL(pressImg);
            Image srcPic = ImageIO.read(url.openStream());
            int pressWidth = srcPic.getWidth(null);
            int pressHeight = srcPic.getHeight(null);
            if (type == 1) {
                //放在左下角
                g.drawImage(srcPic, 0, height - pressHeight, pressWidth, pressHeight, null);
            } else {
                int imageX = x;
                if (imageX > screenshotX) {
                    imageX = screenshotX;
                }
                int imageY = y;
                if (imageY > screenshotY) {
                    imageY = screenshotY;
                }
                g.drawImage(srcPic, imageX, imageY, Math.abs(x - screenshotX), Math.abs(y - screenshotY), null);
            }
            g.dispose();
            FileOutputStream out = new FileOutputStream(targetImg);
            ImageIO.write(image, "jpg", out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 马赛克化.
     *
     * @return
     * @throws Exception
     */
    public static boolean mosaic(String openUrl,
                                 int cx, int cy, int screenshotX, int screenshotY) {
        try {
            int size = 10;
            File file = new File(openUrl);
            Image src = ImageIO.read(file);
            if (!file.isFile()) {
                throw new Exception("ImageDeal>>>" + file + " 不是一个图片文件!");
            }
//            System.out.print("cx :" + cx);
//            System.out.print("cy :" + cy);
//            System.out.print("screenshotX :" + screenshotX);
//            System.out.print("screenshotY :" + screenshotY);
            BufferedImage bi = ImageIO.read(file); // 读取该图片
            BufferedImage spinImage = new BufferedImage(bi.getWidth(),
                    bi.getHeight(), bi.TYPE_INT_RGB);
            if (bi.getWidth() < size || bi.getHeight() < size || size <= 0) { // 马赛克格尺寸太大或太小
                return false;
            }
            int xcount = 0; // 方向绘制个数
            int ycount = 0; // y方向绘制个数
            if ((screenshotX - cx) % size == 0) {
                xcount = (screenshotX - cx) / size;
            } else {
                xcount = (screenshotX - cx) / size + 1;
            }
            if ((screenshotY - cy) % size == 0) {
                ycount = (screenshotY - cy) / size;
            } else {
                ycount = (screenshotY - cy) / size + 1;
            }
            int x = 0; //坐标
            int y = 0;
            // 绘制马赛克(绘制矩形并填充颜色)
            Graphics gs = spinImage.getGraphics();
//            System.out.print("xcount :" + xcount);
//            System.out.print("ycount :" + ycount);
            gs.drawImage(src, 0, 0, bi.getWidth(), bi.getHeight(), null);
            for (int i = 0; i < xcount; i++) {
                for (int j = 0; j < ycount; j++) {
                    //马赛克矩形格大小
                    int mwidth = size;
                    int mheight = size;
                    if (i == xcount - 1) { //横向最后一个比较特殊，可能不够一个size
                        mwidth = (screenshotX - cx) % size;
                        mwidth = mwidth == 0 ? size : mwidth;
                    }
                    if (j == ycount - 1) { //同理
                        mheight = (screenshotY - cy) % size;
                        mheight = mheight == 0 ? size : mheight;
                    }
                    // 矩形颜色取中心像素点RGB值
                    int centerX = cx + (size * i) + (size / 2);
                    int centerY = cy + (size * j) + (size / 2);
                    Color color = new Color(bi.getRGB(centerX, centerY));
                    gs.setColor(color);
                    x = cx + (size * i);
                    y = cy + (size * j);
//                    System.out.print("x:" + x);
//                    System.out.print("y:" + y);
//                    System.out.print("mwidth:" + mwidth);
//                    System.out.println("mheight:" + mheight);
                    gs.fillRect(x, y, mwidth, mheight);
                }
            }
            gs.dispose();

            FileOutputStream out = new FileOutputStream(openUrl);
            ImageIO.write(spinImage, "jpg", out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


        return true;
    }
}
